package com.luyu.caffeinedemo.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;

/**
 * @author luYuHan
 * @date 2023/8/5 16:02
 */
@EnableConfigurationProperties(RepeatSubmitProperty.class)
@Configuration(proxyBeanMethods = false)
public class CaffeineConfig {
    @Autowired
    private MyExpireAfter expireAfter;

    @Bean
    public Cache<String, ArrayList<Cache<String,String>>> getAllCache(RepeatSubmitProperty property) {
        //最外层基于使用总数最大值做到限制,最内层细化到对应的方法粒度进行处理
        return Caffeine.newBuilder()
                // 配置器的存活时间推荐大于
                .expireAfterWrite(property.getTime(),property.getTimeUnit())
                .build();


    }
}
